# coding:utf-8
from django.db import models
from django.contrib.auth.models import User

class Avatar(models.Model):
    path=models.CharField(max_length=100)
    width=models.IntegerField()
    height=models.IntegerField()

class Student(User):
    username = models.CharField(('Nombre de usuario'), max_length=30, unique=True, help_text=("login"))
    first_name = models.CharField(('Nombre'), max_length=30, blank=True)
    last_name = models.CharField(('Apellidos'), max_length=30, blank=True)
    email = models.EmailField(('Correo electrónico'), blank=True)
    sesionTime = models.IntegerField()
    avatar = models.ForeignKey(Avatar)

    #more info
    messenger = models.CharField(('Mensajero instantáneo'), max_length=30, blank=True)
    phone_number = models.CharField(('Número telefónico'), max_length=30, blank=True)
    skype = models.CharField(('Usuario de Skype'), max_length=30, blank=True)
    twitter = models.CharField(('Usuario de Twitter'), max_length=30, blank=True)
    def __unicode__(self):
           return u'S -%s- (%s %s)' % (self.username, self.first_name, self.last_name)


class Course(models.Model):
    name=models.CharField(max_length=50)
    code=models.CharField(max_length=15)
    def __unicode__(self):
        return u'%s -- %s' % (self.code, self.name)

class Teacher(User):
    username = models.CharField(('Usuario'), max_length=30, unique=True, help_text=("Elija el nombre que usará para iniciar sesión"))
    password = models.CharField(('Contraseña'), max_length=128, help_text=("Utilice una contraseña segura"))
    password2 = models.CharField(('Contraseña2'), max_length=128, help_text=("La contraseña debe coincidir"))
    first_name = models.CharField(('Nombre(s)'), max_length=30, blank=False)
    last_name = models.CharField(('Apellidos'), max_length=30, blank=False)
    email = models.EmailField(('Correo electrónico'), blank=False)
    avatar = models.ForeignKey(Avatar)
    hasRegisteredVar = models.BooleanField()

    #datos extra
    location = models.TextField(('Ubicación física'),blank=True)
    phone_number = models.CharField(('Número telefónico'), max_length=30,blank=True)
    skype = models.CharField(('Usuario de Skype'), max_length=30,blank=True)
    messenger = models.CharField(('Mensajero instantáneo'), max_length=30,blank=True)
    homepage = models.URLField(('Su página personal'),blank=True)

    def __unicode__(self):
        return u'T -%s- (%s %s)' % (self.username, self.first_name, self.last_name)

class Group(models.Model):
    code=models.CharField(max_length=15)
    teacher=models.ForeignKey(Teacher)
    course=models.ForeignKey(Course)
    students=models.ManyToManyField(Student)
    
    def __unicode__(self):
        return u'Grupo %s de %s impartido por %s' % (self.code, self.course, self.teacher)

    def get_absolute_url(self):
        return '/course/%s/%s/'%(self.course.code,self.code)

class Thread(models.Model):
    title = models.CharField(('Título'),max_length=50)
    description =models.TextField(('Descripción'))
    date=models.DateTimeField()
    group = models.ForeignKey(Group)
    
    def __unicode__(self):
        return u'Thread Title: %s' % (self.title)

    def get_absolute_url(self):
        return '/thread/%s/'%(self.id)

class Post(models.Model):
    title = models.CharField(('Título'),max_length=50)
    creator=models.ForeignKey(User)
    content = models.TextField()
    rating = models.IntegerField()
    date=models.DateTimeField()
    thread = models.ForeignKey(Thread)

    def __unicode__(self):
        return u'Post Title: %s, creator: %s, rating %s' % (self.title, self.creator, self.rating)
class News(models.Model):
    title = models.CharField(max_length=100)
    date = models.DateTimeField()
    groups = models.ManyToManyField(Group,
                    help_text='''
                    Si desea publicar esta noticia en Otros grupos además de este, selecciónelos.
                    
                                    ''',blank=True)
    content = models.TextField()

    def __unicode__(self):
        return u'%s :: %s'%(self.title, self.content[:50])

    def get_absolute_url(self):
        return '/news/%i/'%(self.id)

class Quiz(models.Model):
    quizId=models.IntegerField()
    date=models.DateTimeField()
    def __unicode__(self):
        return u'%s' % (self.quizId)
    
class AnsweredQuiz(models.Model):
    student = models.ForeignKey(Student)
    quiz = models.ForeignKey(Quiz)
    grade = models.IntegerField()
    
    def __unicode__(self):
        return u'%s answered quiz %s and got %s points' % (str(self.student), str(self.quiz) ,self.grade)

class FeedbackComment(models.Model):
    comment = models.CharField(max_length=500)
    category = models.CharField(max_length=50)
    minGrade = models.IntegerField()

    course = models.CharField(max_length=100)
    group = models.CharField(max_length=100)

    
    def __unicode__(self):
        return u'Comment for a %s with min_grade %d:comment %s, %s %s' % (self.category,self.minGrade,self.comment, self.course, self.group)
        
class File(models.Model):
    title = models.CharField(('Título'),max_length=50)
    filename = models.CharField(max_length=100)
    description = models.TextField(('Descripción'))
    date = models.DateTimeField(('Fecha'))
    allGroups = models.BooleanField(('¿Todos los grupos?'),help_text="El archivo se subirá para todos sus grupos de esta materia")
    groups = models.ManyToManyField(Group)
    def __unicode__(self):
        return u'File Title: %s, Filename: %s, Description: %s' % (self.title,self.filename,self.description[:35])

    def get_absolute_url(self):
        return '/downloadFile/files/%s/'%(self.filename)
 
class Video(models.Model):
    title = models.CharField(('Título'),max_length=50)
    filename = models.CharField(max_length=100)
    description = models.TextField(('Descripción'))
    date = models.DateTimeField(('Fecha'))
    thumbnail = models.CharField(max_length = 100)
    protected = models.BooleanField(('¿Protegido?'),help_text="Se deshabilitará la opción de descargarse")
    allGroups = models.BooleanField(('¿Todos los grupos?'),help_text="Se subirá el video a todos sus grupos de la misma materia")
    groups = models.ManyToManyField(Group)
    def __unicode__(self):
        return u'Video Title: %s, Filename: %s, Description: %s' % (self.title,self.filename,self.description[:35])


class PostUserVote(models.Model):
    post = models.ForeignKey(Post)
    user = models.ForeignKey(User)
    voteType = models.BooleanField()

    def __unicode__(self):
        return u'User %s voted %s on post %s'%(self.user,"Positive" if self.voteType else "Negative",self.post)

class ConstantVariables(models.Model):
    avgHour = models.IntegerField()
    metricDay = models.IntegerField()
    weekDayEnd = models.CharField(max_length=100)
    gradesFile = models.CharField(max_length=100)
    startDate = models.CharField(max_length=10)
    hasStarted = models.BooleanField()
    lstDateUpGrades = models.CharField(max_length=100)
    teacher = models.ForeignKey(Teacher)
    course = models.CharField(max_length=100)
    group = models.CharField(max_length=100)
    def __unicode__(self):
        return u'Constants avgHour: %d metricDay %d weekDayEnd: %s, hasStarted: %s, course: %s' % (self.avgHour,self.metricDay,self.weekDayEnd, self.hasStarted, self.course)

class Subject(models.Model):
    text = models.CharField(max_length=1000)
    def __unicode__(self):
        return u'%s' % (self.text)
    
class Homework(models.Model):
    courseKey = models.CharField(max_length=15)
    groupKey = models.CharField(max_length=15)
    date = models.DateTimeField()
    subjects = models.ForeignKey(Subject)
    def __unicode__(self):
        return u'%s' % (self.subjects)


class News(models.Model):
    title = models.CharField(('Título'),max_length=100)
    date = models.DateTimeField()
    content = models.TextField(('Contenido'))
    groups = models.ManyToManyField(Group,
                    help_text='''
                    Si desea publicar esta noticia en Otros grupos además de este, selecciónelos.

                                    ''',blank=True)
    
    def __unicode__(self):
        return u'%s :: %s'%(self.title, self.content[:50])

    def get_absolute_url(self):
        return '/news/%i/'%(self.id)

class StudentGradeIndex(models.Model):
    courseKey = models.CharField(max_length=15)
    groupKey = models.CharField(max_length=15)
    studentLogin = models.CharField(max_length=30)
    index = models.CharField(max_length=10)

    def __unicode__(self):
        return u'Constants courseKey: %d studentLogin %d: index %s' % (self.courseKey,self.studentLogin,self.index)